---
layout : doc
title : Inversion of Control Support in Phoenix
permalink: docs\IoC.html
---

## Autofac Support

To get autofac support in Phoenix, simply install the autofac extensions package

    PM> Install-Package Phoenix.Extensions.Autofac

Once you do that, open up App.xaml and make sure you are inheriting from AutofacPhoenixApplication rather than PhoenixApplication or Application like follows:

    <Autofac:AutofacPhoenixApplication x:Class="My.App"
                                       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                       xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
                                       xmlns:Autofac="clr-namespace:Phoenix.Extensions.Autofac;assembly=Phoenix.Extensions.Autofac">
        <Application.ApplicationLifetimeObjects>
            <shell:PhoneApplicationService />
        </Application.ApplicationLifetimeObjects>
    </Autofac:AutofacPhoenixApplication>

Then you will have to add another override into your code behind:

    protected override void ConfigureContainer(ContainerBuilder containerBuilder)
    {
        containerBuilder
            .RegisterInstance(new SettingsProvider())
            .As<ISettingsProvider>();
    }

Your controllers will be registered automatically, if they are contained in another assembly use the RegisterControllers extension method in Phoenix.Extensions.Autofac. Like so:

    typeof(MyController).Assembly.RegisterControllers(containerBuilder);
    
If you want your viewmodels registered with your container, you can use the RegisterViewModels extension method in the same manner as the RegisterControllers method.

### IoC Guidance

Generally I do not have the IoC container build my viewmodels, I get my controllers to resolve any services they require, then I build my viewmodels manually inside my controller actions. This is optional obviously, but my viewmodels are not complex enough to need that many services injected into them.